package com.ruoyi.hcrf.domain.service.agv.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.hcrf.bo.agv.InOutPageInBo;
import com.ruoyi.hcrf.domain.entity.task.InOut;
import com.ruoyi.hcrf.domain.mapper.agv.InOutMapper;
import com.ruoyi.hcrf.domain.service.agv.InOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InOutServiceImpl extends ServiceImpl<InOutMapper, InOut> implements InOutService {

    @Autowired
    private InOutMapper inOutMapper;


    /**
     * 分页查询入库出库记录
     *
     * @param pageInBo
     * @return
     */
    @Override
    public Page findAllByPage(InOutPageInBo pageInBo) {
        QueryWrapper<InOut> queryWrapper = new QueryWrapper<>();

        //创建查询构造条件
        getQueryMapper(pageInBo, queryWrapper);

        Page<InOut> page = new Page<>(pageInBo.getPageNum(), pageInBo.getPageSize());

        return inOutMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<InOut> ByPage(InOutPageInBo pageInBo) {
        QueryWrapper<InOut> queryWrapper = new QueryWrapper<>();

        //创建查询构造条件
        getQueryMapperOut(pageInBo, queryWrapper);

        Page<InOut> page = new Page<>(pageInBo.getPageNum(), pageInBo.getPageSize());

        return inOutMapper.selectPage(page, queryWrapper);
    }

    private void getQueryMapperOut(InOutPageInBo pageInBo, QueryWrapper<InOut> queryWrapper) {
//        queryWrapper.eq("del_flag", 0);
        queryWrapper.eq("result", 3);
        queryWrapper.eq("status", 0);

        //
        if (StringUtils.isNotBlank(pageInBo.getItemName()) && StringUtils.isNotBlank(pageInBo.getItemName().trim())) {
            queryWrapper.like("item_name", pageInBo.getItemName());
        }
        if (StringUtils.isNotBlank(pageInBo.getBoxId()) && StringUtils.isNotBlank(pageInBo.getBoxId().trim())) {
            queryWrapper.like("box_id", pageInBo.getBoxId());
        }
        if (pageInBo.getResult() != null) {
            queryWrapper.eq("result", pageInBo.getResult());
        }

        //创建时间排序
        queryWrapper.orderByDesc("create_time");
    }

    /**
     * 查询构造条件
     * @param pageInBo
     * @param queryWrapper
     */
    private void getQueryMapper(InOutPageInBo pageInBo, QueryWrapper<InOut> queryWrapper) {

//        queryWrapper.eq("del_flag", 0);
        queryWrapper.eq("result", 1);
        queryWrapper.eq("status", 0);

        //
        if (StringUtils.isNotBlank(pageInBo.getItemName()) && StringUtils.isNotBlank(pageInBo.getItemName().trim())) {
            queryWrapper.like("item_name", pageInBo.getItemName());
        }
        if (StringUtils.isNotBlank(pageInBo.getBoxId()) && StringUtils.isNotBlank(pageInBo.getBoxId().trim())) {
            queryWrapper.like("box_id", pageInBo.getBoxId());
        }
        if (pageInBo.getResult() != null) {
            queryWrapper.eq("result", pageInBo.getResult());
        }

        //创建时间排序
        queryWrapper.orderByDesc("create_time");

    }
}
